#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
文献筛选程序 - 简化打包脚本
"""
import os
import sys
import subprocess
import shutil
from pathlib import Path

def main():
    """简化打包函数"""
    print("=" * 50)
    print("文献筛选程序 - 简化打包")
    print("=" * 50)
    
    # 设置环境变量
    os.environ['PYTHONIOENCODING'] = 'utf-8'
    
    # 检查PyInstaller
    try:
        import PyInstaller
        print(f"✓ PyInstaller版本: {PyInstaller.__version__}")
    except ImportError:
        print("✗ PyInstaller未安装")
        return False
    
    # 清理旧文件
    print("\n清理旧文件...")
    for dir_name in ['build', 'dist']:
        if os.path.exists(dir_name):
            shutil.rmtree(dir_name)
    
    # 简化的打包命令，只排除最关键的模块
    cmd = [
        "pyinstaller",
        "--name=文献筛选",
        "--onefile",
        "--windowed",
        "--clean",
        "--noconfirm",
        # 只排除最关键的模块
        "--exclude-module=matplotlib",
        "--exclude-module=numpy",
        "--exclude-module=pandas",
        "--exclude-module=scipy",
        "--exclude-module=skimage",
        "--exclude-module=IPython",
        "--exclude-module=jupyter",
        "--exclude-module=notebook",
        "--exclude-module=PyQt5",
        "--exclude-module=bokeh",
        "--exclude-module=plotly",
        "--exclude-module=sympy",
        "--exclude-module=statsmodels",
        "--exclude-module=xarray",
        "--exclude-module=dask",
        "--exclude-module=h5py",
        "--exclude-module=astropy",
        "gui_main.py"
    ]
    
    print(f"\n执行打包命令:")
    print(" ".join(cmd))
    print("\n开始打包...")
    
    try:
        # 执行打包
        result = subprocess.run(cmd, check=True, text=True, encoding='utf-8')
        print("✓ 打包成功!")
        
        # 检查结果
        exe_path = Path("dist/文献筛选.exe")
        if exe_path.exists():
            file_size = exe_path.stat().st_size / (1024 * 1024)
            print(f"\n✓ 生成文件: {exe_path}")
            print(f"文件大小: {file_size:.1f} MB")
            print(f"\n🎉 打包完成!")
            return True
        else:
            print("✗ 文件生成失败")
            return False
            
    except subprocess.CalledProcessError as e:
        print(f"✗ 打包失败: {e}")
        return False
    except Exception as e:
        print(f"✗ 错误: {e}")
        return False

if __name__ == "__main__":
    success = main()
    if not success:
        sys.exit(1) 